﻿using PF;
using UnityEngine;
using Quaternion = UnityEngine.Quaternion;
using Vector3 = UnityEngine.Vector3;
using ETModel;

namespace ETHotfix {
    [ObjectSystem]
    public class UnitAwakeSystem : AwakeSystem<Unit, GameObject> {
        public override void Awake(Unit self, GameObject gameObject) {
            self.Awake(gameObject);
        }
    }

    /// <summary>
    /// ET框架的单位基类,创建方法:<para/>
    /// ComponentFactory.CreateWithId&lt;Unit, GameObject&gt;(服务端分配的单位ID, Unity的GameObject对象)<para/>
    /// Unit.Dispose()调用,传入的GameObject同时销毁<para/>
    /// </summary>
    [HideInHierarchy]
    public class Unit : Entity {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameObject">传进来,GameUnit里负责销毁</param>
        /// <param name="unitInfo"></param>
		public virtual void Awake(GameObject gameObject) {
            this.GameObject = gameObject;
            this.GameObject.AddComponent<ComponentView>().Component = this;
        }
        public override void Dispose() {
            if (this.IsDisposed) {
                return;
            }
            UnityEngine.Object.Destroy(GameObject);

            base.Dispose();
        }

        public virtual Vector3 Position {
            get {
                return GameObject?.transform?.position ?? Vector3.zero;
            }
            set {
                if (GameObject != null && GameObject.transform != null) {
                    GameObject.transform.position = value;
                }
            }
        }

        public virtual Quaternion Rotation {
            get {
                return GameObject?.transform?.rotation ?? Quaternion.identity;
            }
            set {
                if (GameObject != null && GameObject.transform != null) {
                    GameObject.transform.rotation = value;
                }
            }
        }

        public virtual Vector3 Scale {
            get {
                return GameObject?.transform?.localScale ?? Vector3.zero;
            }
            set {
                if (GameObject != null && GameObject.transform != null) {
                    GameObject.transform.localScale = value;
                }
            }
        }
    }
}